//
//  HMWelcomeViewController.swift
//  WeiboTT
//
//  Created by 王嘉涛 on 2016/11/16.
//  Copyright © 2016年 王嘉涛. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class HMWelcomeViewController: UIViewController {

    // 懒加载用户未登录时的站位图片
    private lazy var iconImage : UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    // 描述内容
    private lazy var welLbl : UILabel = UILabel(title: "欢迎再次归来!", fontSize: 13, textColor: #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1))
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    // 动画的执行需要在
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        animationLoad()
    }
    
    
    // 设置动画方法
    private func animationLoad(){
        let offsetXY = -(UIScreen.main.bounds.height - 250 - iconImage.bounds.height)
        
        // 更新约束
        iconImage.snp.updateConstraints { (make) in
            make.bottom.equalTo(self.view).offset(offsetXY)
        }
        
        // 执行动画方法
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 7, options: [], animations: {
            //执行动画效果
            //更新约束
            //提前刷新页面
            self.view.layoutIfNeeded()
            
        }) { (finished) in
            //动画效果完成的回调
            UIView.animate(withDuration: 0.5, animations: {
                self.welLbl.alpha = 1
            }, completion: { (_) -> () in
                print("👌")
                UIApplication.shared.keyWindow?.rootViewController = XLMainTabBarController()
                NotificationCenter.default.post(name: NSNotification.Name(kChoseRootViewController), object: nil)
            })
        }
    }

    // 设置UI
    private func setupUI(){
        // 设置背景颜色
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        // 添加两个子控件
        view.addSubview(iconImage)
        view.addSubview(welLbl)
        // 设置约束
        iconImage.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-120)
        }
        
        welLbl.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImage)
            make.top.equalTo(iconImage.snp.bottom).offset(20)
        }
        
        // 隐藏文字
        welLbl.alpha = 0
        
        // 设置图片的圆角
        iconImage.layer.cornerRadius = 135 * 0.5
        iconImage.layer.masksToBounds = true
        
        // 设置网络图片
        iconImage.sd_setImage(with: HMUserAccountViewModel.shared.headURL)

    }

}
